<template>
  <div class="my-tag" @dblclick="showInput">
    <input
      class="input"
      type="text"
      placeholder="输入标签"
      v-if="edit"
      v-model="tag"
      v-focus
      @blur="closeInput"
      @keyup.enter="closeInput"
    />
    <div class="text" v-else>{{ value ? value : '无标签' }}</div>
  </div>
</template>

<script>
export default {
  name: 'MyTag',
  props: {
    value: String,
  },
  data() {
    return {
      edit: false,
      tag: '',
    }
  },

  methods: {
    //双击以后展示输入框
    showInput() {
      this.edit = true
      //当输入框显示的时候将value值给到tag
      this.tag = this.value
    },

    //失焦以后隐藏输入框
    closeInput() {
      this.edit = false

      //当输入框显示的时候触发父组件上的input事件（v-model），将value的值修改为tag
      this.$emit('input', this.tag)
    },
  },
}
</script>

<style scoped lang="less">
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>